﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace RequirementAndBug.Web.Models
{
    public class AuthenticationAttribute : ActionFilterAttribute
    {
        public AuthenticationAttribute()
        {
            this.IsUsedAttribute = true;
        }
        /// <summary>
        /// 是否启用session验证
        /// </summary>
        public bool IsUsedAttribute
        {
            get;
            set;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            if (filterContext.HttpContext.Session["UserID"] == null || string.IsNullOrWhiteSpace(filterContext.HttpContext.Session["UserID"].ToString()))
            {
                if (IsUsedAttribute)
                {
                    filterContext.Result = new RedirectToRouteResult("Default", new System.Web.Routing.RouteValueDictionary(new
                    {
                        action = "Login",
                        controller = "User"
                    }));
                }
            }  
            base.OnActionExecuting(filterContext);
        }
    }
}